/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.main;

import com.koron.util.Constant;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.swan.bean.MessageBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class AuthInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(request.getSession().getAttribute(Constant.USER)==null){
            MessageBean<?> msg = MessageBean.create(Constant.MESSAGE_INT_NOLOGIN,"用户信息已过期，请重新登录",null);
            response.setContentType("text/json;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.print(msg.toJson());
            writer.close();
            response.flushBuffer();
            return false;
        }
        request.getSession().setMaxInactiveInterval(28800);
        return true;
    }
}
